<?php
declare(strict_types=1);
/**
 * Created by project.
 * User: huanzipap
 * Date: 2023/5/11
 * Time: 15:01
 */

namespace App\Model;

use Hyperf\Database\Model\Model;
use Hyperf\Database\Model\SoftDeletes;


class News extends Model
{
    use SoftDeletes;

    protected ?string $table='news';
    public bool $timestamps = true;

    protected array $fillable = [
        'party_id','type','title','photo','content','media_list','media_list','is_banner','banner_photo',
        'link_id','link_url','is_long','show_start_at','show_end_at','status','admin_id'
    ];

    public function user(): \Hyperf\Database\Model\Relations\belongsTo
    {
        return $this->belongsTo(User::class, 'uid', 'id');
    }

    public function party(): \Hyperf\Database\Model\Relations\BelongsTo
    {
        return $this->belongsTo(Party::class, 'party_id', 'id');
    }

    public function comment(): \Hyperf\Database\Model\Relations\hasMany
    {
        return $this->hasMany(NewsComment::class, 'news_id', 'id');
    }


    public function joinStatus(): \Hyperf\Database\Model\Relations\hasOne
    {
        return $this->hasOne(NewsJoin::class, 'news_id', 'id');
    }

    public function getContentAttribute($value): string
    {
        return $value != null ? htmlspecialchars_decode($value) : '';
    }


    public function setContentAttribute($value): void
    {
        if($value!='') htmlspecialchars($value);
        $this->attributes['content'] = $value;
    }


    public function setFileListAttribute($value): void
    {
        $this->attributes['file_list'] = json_encode($value);
    }

    public function getFileListAttribute($value): array
    {
        return $value != null ? json_decode($value,true) : [];
    }


    public function setMediaListAttribute($value)
    {
        $this->attributes['media_list'] = implode(',', $value);
    }

    public function getMediaListAttribute($value): array
    {
        return $value != null ? explode(',', $value) : [];
    }

}